package com.neuedu.controller.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuedu.common.ResultBean;
import com.neuedu.factory.ClassFactory;
import com.neuedu.service.TableService;
import com.neuedu.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UserServlet", urlPatterns = "/his/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultBean rsb = new ResultBean();
        String pageStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize = Integer.parseInt(pageSizeStr);
        int pageNum = 1;
        if (pageStr == null || pageStr.length() == 0) {
            pageNum = 1;
        } else {
            try {
                pageNum = Integer.parseInt(pageStr);
            } catch (Exception e) {
                pageNum = 1;
            }
        }
        Object[] objs = {pageNum, pageSize};
        ObjectMapper om = new ObjectMapper();
        UserService us = (UserService) ClassFactory.createObj("UserService");
        List<Map<String, Object>> list = us.userGet(objs);
        rsb.setList(list);
        rsb.setCount(us.getCount());
        String s = om.writeValueAsString(rsb);
        PrintWriter writer = response.getWriter();
        writer.append(s);
    }
}
